<?php
require('functions/page_load.php');

$users_nav = true;

if($_SERVER['REQUEST_METHOD'] == 'POST'){

	if($_POST['user_address'] == '0'){

	// insert address
	
	$address = $_POST['line_1'].",".$_POST['line_2'].",".$_POST['line_3'].",".$_POST['postcode'];
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "http://maps.google.com/maps/geo?q=".urlencode($address)."&output=csv&oe=utf8&sensor=false&key=".$settings['google_api_key']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($ch);
		curl_close($ch);
	$outputArr = explode(",",$output);
	$latitude = $outputArr[2];
	$longitude = $outputArr[3];
	
	$sql = "INSERT INTO addresses SET 
			area_id = '".mysql_real_escape_string($_POST['area_id'])."', 
			line_1 = '".mysql_real_escape_string($_POST['line_1'])."', 
			line_2 = '".mysql_real_escape_string($_POST['line_2'])."', 
			line_3 = '".mysql_real_escape_string($_POST['line_3'])."', 
			postcode = '".mysql_real_escape_string($_POST['postcode'])."',
			latitude = '".$latitude."',
			longitude = '".$longitude."',
			user_id = '".mysql_real_escape_string($_POST['user_id'])."'";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
	$new_address_id = mysql_insert_id();
	
	}
	
	else{
	
	$new_address_id = $_POST['user_address'];
	
	}

	// insert job
	
	$sql = "INSERT INTO jobs SET 
			user_id = '".mysql_real_escape_string($_POST['user_id'])."', 
			address_id = '".$new_address_id."', 
			area_id = '".mysql_real_escape_string($_POST['area_id'])."', 
			trade_id = '".mysql_real_escape_string($_POST['trade_id'])."', 
			title = '".mysql_real_escape_string($_POST['title'])."', 
			description = '".mysql_real_escape_string($_POST['description'])."', 
			status = '".mysql_real_escape_string($_POST['status'])."', 
			job_active = '".mysql_real_escape_string($_POST['job_active'])."', 
			materials = '".mysql_real_escape_string($_POST['materials'])."'";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
	$new_job_id = mysql_insert_id();
	
	header("Location: edit_job.php?id=".$new_job_id);

}

$sql = "SELECT firstname, surname, address_id, line_1, line_2, line_3, postcode, area FROM users 
		LEFT JOIN addresses ON users.address_id = addresses.id 
		LEFT JOIN areas ON addresses.area_id = areas.id 
		WHERE users.id = '".mysql_real_escape_string($_GET['user_id'])."'";
$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
$rs = mysql_fetch_assoc($query);

$address = (strlen($rs['line_2']) > 0) ? $rs['line_1'].', '.$rs['line_2'].', ' : $rs['line_1'].', ' ;
$address.= (strlen($rs['line_3']) > 0) ? $rs['line_3'].', '.$rs['area'].' '.$rs['postcode'] : $rs['area'].' '.$rs['postcode'] ;

require('includes/header.php');
?>

<div class="side_container">
<h2 class="container_header">Filters</h2>

<ul>
<li><a href="users.php">Active users</a></li>
<li><a href="users.php?show=banned">Banned users</a></li>
</ul>

</div>

<div class="main_container_wrapper">

	<div class="main_container">
	<h2 class="container_header">Add Job for <?php echo $rs['firstname'].' '.$rs['surname'] ;?></h2>
	
	<div id="top_links">
	<a href="users.php?id=<?php echo $_GET['user_id']; ?>" style="background-image: url(images/back.png); padding-left: 20px;">Back to <?php echo $rs['firstname'].' '.$rs['surname'] ;?></a>
	</div>
	
	<?php echo $results; ?>
	
			<form method="post" action="">
			
			<input type="hidden" name="user_id" value="<?php echo $_GET['user_id']; ?>" />
			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right">
			<input type="radio" name="user_address" id="user_address" value="0" onclick="document.getElementById('address_container').style.display = (this.checked == true) ? 'block' : 'none' ;" /> <label for="user_address" style="font-size: 11px; position: relative; top: -2px;">Create new address</label><br />
			<?php
			
			$sql = "SELECT addresses.id, line_1, line_2, line_3, postcode, area FROM addresses 
					LEFT JOIN areas ON addresses.area_id = areas.id 
					WHERE user_id = '".mysql_real_escape_string($_GET['user_id'])."'";
			$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
			while($address_rs = mysql_fetch_assoc($query)){

			$address = (strlen($address_rs['line_2']) > 0) ? $address_rs['line_1'].', '.$address_rs['line_2'].', ' : $address_rs['line_1'].', ' ;
			$address.= (strlen($address_rs['line_3']) > 0) ? $address_rs['line_3'].', '.$address_rs['area'].' '.$address_rs['postcode'] : $address_rs['area'].' '.$address_rs['postcode'] ;
			
			echo '<input type="radio" name="user_address" id="user_address_'.$address_rs['id'].'" value="'.$address_rs['id'].'" onclick="document.getElementById(\'address_container\').style.display = (this.checked == true) ? \'none\' : \'block\' ;" /> <label for="user_address_'.$address_rs['id'].'" style="font-size: 11px; position: relative; top: -2px;">Use '.$address.'</label><br />';
			
			}
			
			?>
			</div>
			</div>			
			
			<div id="address_container">
			
				<div class="form_row_wrapper">
				<div class="form_row_left"><label for="line_1">Address:</label></div>
				<div class="form_row_right">
				<input type="text" name="line_1" id="line_1" class="textbox" value="" />
				<input type="text" name="line_2" id="line_2" class="textbox" value="" />
				<input type="text" name="line_3" id="line_3" class="textbox" value="" />
				<select name="area_id" id="area_id" style="margin-top: 5px;" class="textbox clearLeft">
				<?php
				
				$sql = "SELECT id, area FROM areas ORDER BY area";
				$area_query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
				while($area_rs = mysql_fetch_assoc($area_query)){
				
				echo '<option value="'.$area_rs['id'].'">'.$area_rs['area'].'</option>';
				
				}
				
				?></select>
				<input type="text" name="postcode" id="postcode" class="textbox clearLeft" value="" style="width: 80px;" />
				</div>
				</div>
			
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="title">Job Title:</label></div>
			<div class="form_row_right"><input type="text" name="title" id="title" class="textbox" value="<?php echo $rs['title']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="description">Job Description:</label></div>
			<div class="form_row_right"><textarea name="description" id="description" rows="6" cols="30" class="textbox" style="width: 520px; height: 200px;"><?php echo $rs['description']; ?></textarea></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="trade_id">Trade:</label></div>
			<div class="form_row_right"><select name="trade_id" id="trade_id">
			<?php
			
			$sql = "SELECT id, trade FROM trades ORDER BY trade";
			$area_query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
			while($trade_rs = mysql_fetch_assoc($area_query)){
			
			$selected = ($trade_rs['id'] == $rs['trade_id']) ? ' selected="selected"' : '' ;
			echo '<option value="'.$trade_rs['id'].'"'.$selected.'>'.$trade_rs['trade'].'</option>';
			
			}
			
			?>
			</select></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="materials">Materials supplied:</label></div>
			<div class="form_row_right"><input type="checkbox" name="materials" id="materials" value="1"<?php echo ($rs['materials'] == '1') ? ' checked="checked"' : '' ; ?> /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="status">Status:</label></div>
			<div class="form_row_right"><select name="status" id="status">
			<option value="Pending"<?php echo ($rs['status'] == 'Pending') ? ' selected="selected"' : '' ; ?>>Pending</option>
			<option value="Closed"<?php echo ($rs['status'] == 'Closed') ? ' selected="selected"' : '' ; ?>>Closed</option>
			<option value="Open"<?php echo ($rs['status'] == 'Open') ? ' selected="selected"' : '' ; ?>>Open</option>
			</select></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="job_active">Job active:</label></div>
			<div class="form_row_right"><input type="checkbox" name="job_active" id="job_active" value="1"<?php echo ($rs['job_active'] == '1') ? ' checked="checked"' : '' ; ?> /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>
		
	</div>

</div>
	
<?php
	require('includes/footer.php');
?>